<template>
  <div class="component-upload-image">
    <el-upload
      :action="uploadImgUrl"
      list-type="picture-card"
      :on-success="handleUploadSuccess"
      :before-upload="handleBeforeUpload"
      :on-error="handleUploadError"
      name="file"
      :headers="headers"
      :file-list="currentFileList"
      style="display: inline-block; vertical-align: top"
      :data="defaultPrarms"
      :limit="limit"
      :on-exceed="handleExceed"
    >
      <i slot="default" v-if="!disabled" class="el-icon-plus"></i>
      <div slot="file" slot-scope="{file}" style="height: 100%;">
        <img
          class="el-upload-list__item-thumbnail"
          :src="file.url|imgurl" alt=""
        >
        <span class="el-upload-list__item-actions">
          <span
            class="el-upload-list__item-preview"
            @click="handlePictureCardPreview(file)"
          >
            <i class="el-icon-zoom-in"></i>
          </span>
          <span
            v-if="!disabled"
            class="el-upload-list__item-delete"
            @click="handleRemove(file)"
          >
            <i class="el-icon-delete"></i>
          </span>
        </span>
      </div>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible" title="预览" width="800" append-to-body>
      <img :src="dialogImageUrl|imgurl" style="display: block; max-width: 100%; margin: 0 auto;">
    </el-dialog>
  </div>
</template>

<script>
  import { getToken } from "@/utils/auth";

  export default {
    data() {
      return {
        uploadImgUrl:`${process.env.VUE_APP_BASE_API}/common/minioUpload`, // 上传的图片服务器地址
        headers: {
          Authorization: "Bearer " + getToken(),
        },
        currentFileList: this.fileList,
        dialogVisible: false,
        dialogImageUrl:'',
      };
    },
    props: {
      fileList: {
        type: Array,
        default:()=>[],
      },
      defaultPrarms: {
        type: Object,
        default: ()=>{},
      },
      modelType: {
        type: String,
        default: 'common',
      },
      limit: {
        type: Number,
        default: 10,
      },
      disabled: {
        type: Boolean,
        default: false
      },
    },
    methods: {
      handlePictureCardPreview(file) {
        this.dialogImageUrl = file.url;
        this.dialogVisible = true;
      },
      handleRemove(file){
        let index = this.currentFileList.findIndex(x=>x.url == file.url )
        if(index != -1){
          this.fileList.splice(index, 1);
        }
      },
      handleUploadSuccess(res) {
        this.currentFileList.push({name:res.fileName,url:res.url});
        this.loading.close();
      },
      handleBeforeUpload() {
        this.loading = this.$loading({
          lock: true,
          text: "上传中",
          background: "rgba(0, 0, 0, 0.7)",
        });
      },
      handleUploadError() {
        this.$message({
          type: "error",
          message: "上传失败",
        });
        this.loading.close();
      },
      handleExceed(files, fileList) {
        this.$message.warning(`当前限制选择 ${this.limit} 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
      },
      getFileList(){
        return this.currentFileList;
      },
      getFileSrcs(){
        let srcs = [];
        if(this.currentFileList && this.currentFileList.length > 0){
          for(let i in this.currentFileList){
            let row = this.currentFileList[i];
            srcs.push(row.url);
          }
        }
        return srcs.join(";");
      }
    },
    watch: {
      fileList(v){
        this.currentFileList = v;
      }
    },
  };
</script>

<style scoped lang="scss">
  .image {
    position: relative;
    .mask {
      opacity: 0;
      position: absolute;
      top: 0;
      width: 100%;
      background-color: rgba(0, 0, 0, 0.5);
      transition: all 0.3s;
    }
    &:hover .mask {
      opacity: 1;
    }
  }
</style>
